﻿using BacklogParser.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace BacklogParser.Parsers
{
    public static class AddressBookParser
    {
        public static AddressBook Parse(XElement addressBook)
        {
            var name = addressBook.Descendants(Constants.Epo + "name").First().Value;
            var address1 = addressBook.Descendants(Constants.Epo + "address-1").FirstOrDefault();
            var address2 = addressBook.Descendants(Constants.Epo + "address-2").FirstOrDefault();

            var address3 = addressBook.Descendants(Constants.Epo + "address-3").FirstOrDefault();
            var address4 = addressBook.Descendants(Constants.Epo + "address-4").FirstOrDefault();
            var address5 = addressBook.Descendants(Constants.Epo + "address-5").FirstOrDefault();


            var country = addressBook.Descendants(Constants.Epo + "country").FirstOrDefault();

            var idAttr = addressBook.Attribute("cdsid");

            string addr1 = "";
            string addr2 = "";
            string addr3 = "";
            string addr4 = "";
            string addr5 = "";

            string cntry = "";
            string id = "";

            if(idAttr != null)
            {
                id = idAttr.Value;
            }

            if (address1 != null)
                addr1 = address1.Value;

            if (address2 != null)
                addr2 = address2.Value;

            if (address3 != null)
                addr3 = address3.Value;

            if (address4 != null)
                addr4 = address4.Value;

            if (address4 != null)
                addr4 = address4.Value;

            if (address5 != null)
                addr5 = address5.Value;

            if (country != null)
                cntry = country.Value;

            return new AddressBook
            {
                CdsId = id,
                Name = name,
                Address1 = addr1,
                Address2 = addr2,
                Address3 = addr3,
                Address4 = addr4,
                Address5 = addr5,
                Country = cntry
            };
        }
    }
}
